<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-row :gutter="20" justify="space-between">
        <el-col :span="6">
          <el-form-item label="姓名" prop="hzName">
            <el-input
              v-model="queryParams.hzName"
              placeholder="请输入姓名"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="年龄" prop="hzAge">
            <el-input
              v-model="queryParams.hzAge"
              placeholder="请输入年龄"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="挂号类型" prop="hzLevel">
            <el-input
              v-model="queryParams.hzLevel"
              placeholder="请输入挂号类型"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="看诊医生" prop="hzDoc">
            <el-input
              v-model="queryParams.hzDoc"
              placeholder="请输入看诊医生"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" justify="space-between">
        <el-col :span="6">
          <el-form-item label="身份证号" prop="hzCard">
            <el-input
              v-model="queryParams.hzCard"
              placeholder="请输入身份证号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="挂号时间" prop="hzTime">
            <el-date-picker
              clearable
              size="small"
              v-model="queryParams.hzTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择挂号时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="诊断号" prop="hzNum">
            <el-input
              v-model="queryParams.hzNum"
              placeholder="请输入诊断号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="就诊状态" prop="hzState">
            <el-input
              v-model="queryParams.hzState"
              placeholder="请输入就诊状态"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-form-item label="科室" prop="hzDpet">
        <el-input
          v-model="queryParams.hzDpet"
          placeholder="请输入科室"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="遍历本" prop="hzBook">
        <el-input
          v-model="queryParams.hzBook"
          placeholder="请输入遍历本"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="住址" prop="hzAddress">
        <el-input
          v-model="queryParams.hzAddress"
          placeholder="请输入住址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="联系方式" prop="hzCall">
        <el-input
          v-model="queryParams.hzCall"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="性别" prop="hzSix">
        <el-input
          v-model="queryParams.hzSix"
          placeholder="请输入性别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-row>
        <el-col :span="4" :push="20">
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="medium"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="medium" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['qiantai:user:add']"
          >新增</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="titleUpdate"
          v-hasPermi="['qiantai:user:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['qiantai:user:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['qiantai:user:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
      border
      stripe
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" width="50" type="index" />
      <!-- <el-table-column label="ID" align="center" prop="hzId" /> -->
      <el-table-column label="姓名" width="100" align="center" prop="hzName" />
      <el-table-column label="年龄" width="80" align="center" prop="hzAge" />
      <el-table-column label="性别" width="80" align="center" prop="hzSix">
        <template slot-scope="scope">
          <span>{{ scope.row.hzSix == "1" ? "男" : "女" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="科室" align="center" prop="hzDpet" width="150" />
      <el-table-column
        label="挂号类型"
        width="90"
        align="center"
        prop="hzLevel"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.hzLevel == "1" ? "专家" : "普通" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="看诊医生" align="center" prop="hzDoc" />
      <el-table-column label="病历本" width="60" align="center" prop="hzBook">
        <template slot-scope="scope">
          <span>{{ scope.row.hzBook == "1" ? "有" : "无" }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="身份证号" align="center" prop="hzCard" /> -->
      <!-- <el-table-column label="住址" align="center" prop="hzAddress" /> -->
      <!-- <el-table-column label="联系方式" align="center" prop="hzCall" /> -->
      <el-table-column
        label="挂号时间"
        width="150"
        align="center"
        prop="hzTime"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hzTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="诊断号" align="center" prop="hzNum" width="160" />
      <el-table-column label="就诊状态" align="center" prop="hzState">
        <template slot-scope="scope">
          <span>{{
            scope.row.hzState == 0
              ? "就诊中"
              : scope.row.hzState == "1"
              ? "已就诊"
              : scope.row.hzState == "2"
              ? "复诊中"
              : "已退费"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="titleUpdate(scope.row)"
            v-hasPermi="['qiantai:user:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['qiantai:user:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改患者信息表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="hzName">
          <el-input v-model="form.hzName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="年龄" prop="hzAge">
          <el-input v-model="form.hzAge" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="一级科室">
          <el-select
            v-model="onedept"
            placeholder="请输入挂号科室"
            clearable
            :style="{ width: '100%' }"
            @change="getDept"
          >
            <el-option
              v-for="(item, index) in deptList"
              :key="index"
              :label="item.deptName"
              :value="item.deptId"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级科室">
          <el-select
            v-model="form.hzDpet"
            placeholder="请输入挂号科室"
            clearable
            :style="{ width: '100%' }"
            @change="getDoc"
          >
            <el-option
              v-for="(item, index) in deptList2"
              :key="index"
              :label="item.deptName"
              :value="item.deptName"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="挂号类型">
          <el-select
            @change="getDoc"
            v-model="form.hzLevel"
            placeholder="请输入挂号级别"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in hzLevelOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="看诊医生">
          <el-select
            v-model="form.hzDoc"
            placeholder="请选择看诊医生"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in hzDocOptions"
              :key="index"
              :label="item.docName"
              :value="item.docName"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="病历本" prop="hzBook">
          <!-- <el-input v-model="form.hzBook" placeholder="请输入遍历本" /> -->
          <el-select v-model="form.hzBook" placeholder="请选择">
            <el-option
              v-for="item in hzBookOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号" prop="hzCard">
          <el-input v-model="form.hzCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="住址" prop="hzAddress">
          <el-input v-model="form.hzAddress" placeholder="请输入住址" />
        </el-form-item>
        <el-form-item label="联系方式" prop="hzCall">
          <el-input v-model="form.hzCall" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="挂号时间" prop="hzTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.hzTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择挂号时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="性别" prop="hzSix">
          <!-- <el-input v-model="form.hzSix" placeholder="请输入性别" /> -->
          <el-select v-model="form.hzSix" placeholder="请选择">
            <el-option
              v-for="item in hzSixOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="诊断号" prop="hzNum">
          <el-input v-model="form.hzNum" placeholder="请输入诊断号" />
        </el-form-item>
        <el-form-item label="就诊状态" prop="hzState">
          <!-- <el-input v-model="form.hzState" placeholder="请输入就诊状态" /> -->
          <el-select v-model="form.hzState" placeholder="请选择">
            <el-option
              v-for="item in hzStateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-drawer
      :title="title"
      :visible.sync="dialog"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
      size="1000"
    >
      <div class="demo-drawer__content">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="姓名" prop="hzName">
            <el-input v-model="form.hzName" placeholder="请输入姓名" />
          </el-form-item>
          <el-form-item label="年龄" prop="hzAge">
            <el-input v-model="form.hzAge" placeholder="请输入年龄" />
          </el-form-item>
          <el-form-item label="一级科室">
            <el-select
              v-model="onedept"
              placeholder="请输入挂号科室"
              clearable
              :style="{ width: '100%' }"
              @change="getDept"
            >
              <el-option
                v-for="(item, index) in deptList"
                :key="index"
                :label="item.deptName"
                :value="item.deptId"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="二级科室">
            <el-select
              v-model="form.hzDpet"
              placeholder="请输入挂号科室"
              clearable
              :style="{ width: '100%' }"
              @change="getDoc"
            >
              <el-option
                v-for="(item, index) in deptList2"
                :key="index"
                :label="item.deptName"
                :value="item.deptName"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="挂号类型">
            <el-select
              @change="getDoc"
              v-model="form.hzLevel"
              placeholder="请输入挂号级别"
              clearable
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="(item, index) in hzLevelOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="看诊医生">
            <el-select
              v-model="form.hzDoc"
              placeholder="请选择看诊医生"
              clearable
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="(item, index) in hzDocOptions"
                :key="index"
                :label="item.docName"
                :value="item.docName"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="病历本" prop="hzBook">
            <!-- <el-input v-model="form.hzBook" placeholder="请输入遍历本" /> -->
            <el-select v-model="form.hzBook" placeholder="请选择">
              <el-option
                v-for="item in hzBookOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="身份证号" prop="hzCard">
            <el-input v-model="form.hzCard" placeholder="请输入身份证号" />
          </el-form-item>
          <el-form-item label="住址" prop="hzAddress">
            <el-input v-model="form.hzAddress" placeholder="请输入住址" />
          </el-form-item>
          <el-form-item label="联系方式" prop="hzCall">
            <el-input v-model="form.hzCall" placeholder="请输入联系方式" />
          </el-form-item>
          <el-form-item label="挂号时间" prop="hzTime">
            <el-date-picker
              clearable
              size="small"
              v-model="form.hzTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择挂号时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="性别" prop="hzSix">
            <!-- <el-input v-model="form.hzSix" placeholder="请输入性别" /> -->
            <el-select v-model="form.hzSix" placeholder="请选择">
              <el-option
                v-for="item in hzSixOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="诊断号" prop="hzNum">
            <el-input v-model="form.hzNum" disabled placeholder="请输入诊断号" />
          </el-form-item>
          <el-form-item label="就诊状态" prop="hzState">
            <!-- <el-input v-model="form.hzState" placeholder="请输入就诊状态" /> -->
            <el-select v-model="form.hzState" placeholder="请选择">
              <el-option
                v-for="item in hzStateOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="cancelForm">取 消</el-button>
          <el-button
            type="primary"
            @click="closeDrawer(form.hzId)"
            :loading="loading"
            >{{ loading ? "提交中 ..." : "确 定" }}</el-button
          >
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
} from "@/api/qiantai/user";
import { listDept } from "@/api/system/dept";
import { listDocList } from "@/api/doclist/docList";
export default {
  name: "User",
  data() {
    return {
      title: null,
      dialog: false,
      hzDocOptions: null,
      onedept: null,
      deptList: null,
      hzLevelOptions: [
        {
          value: "1",
          label: "专家",
        },
        {
          value: "0",
          label: "普通",
        },
      ],
      hzBookOptions: [
        {
          value: "1",
          label: "有",
        },
        {
          value: "0",
          label: "无",
        },
      ],
      hzSixOptions: [
        {
          value: "1",
          label: "男",
        },
        {
          value: "0",
          label: "女",
        },
      ],
      hzStateOptions: [
        {
          value: "0",
          label: "就诊中",
        },
        {
          value: "1",
          label: "已就诊",
        },
        {
          value: "2",
          label: "复诊中",
        },
        {
          value: "3",
          label: "已退费",
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 患者信息表表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hzName: null,
        hzAge: null,
        hzDpet: null,
        hzLevel: null,
        hzDoc: null,
        hzBook: null,
        hzCard: null,
        hzAddress: null,
        hzCall: null,
        hzTime: null,
        hzSix: null,
        hzNum: null,
        hzState: null,
      },
      deptList2: null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hzName: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        hzAge: [{ required: true, message: "年龄不能为空", trigger: "blur" }],
        hzCard: [
          { required: true, message: "身份证号错误", trigger: "blur",pattern:
              /^((\d{18})|([0-9x]{18})|([0-9X]{18}))$/, },
        ],
        hzCall: [
          { required: true, message: "联系方式错误", trigger: "blur",pattern:
              /^(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])\d{8}$/, },
        ],
      },
    };
  },
  created() {
    this.getList();
    listDept({ parentId: 100 }).then((res) => {
      this.deptList = res.data;
      console.log(this.deptList);
    });
  },
  methods: {
    cancelForm() {
      this.reset();
      this.dialog = false;
    },
    closeDrawer(row) {
      updateUser(this.form).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.dialog = false;
        this.getList();
      });
    },
    titleUpdate(row) {
      this.dialog = true;
      this.title = row.hzName + "信息表";
      this.reset();
      const hzId = row.hzId || this.ids;
      getUser(hzId).then((response) => {
        this.form = response.data;
      });
    },
    // 获取医生列表
    getDoc() {
      this.form.hzDoc = null;
      this.$nextTick(() => {
        listDocList({
          docDept: this.form.hzDpet,
          docLevel: this.form.hzLevel,
        }).then((res) => {
          console.log(this.form.hzDpet, res);
          this.hzDocOptions = res.rows;
        });
      });
    },
    getDept() {
      this.form.hzDpet = null;
      this.form.hzLevel = null;
      this.form.hzDoc = null;
      this.$nextTick(() => {
        listDept({ parentId: this.onedept }).then((res) => {
          this.deptList2 = res.data;
          console.log(this.deptList2);
        });
      });
    },
    /** 查询患者信息表列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hzId: null,
        hzName: null,
        hzAge: null,
        hzDpet: null,
        hzLevel: null,
        hzDoc: null,
        hzBook: null,
        hzCard: null,
        hzAddress: null,
        hzCall: null,
        hzTime: null,
        hzSix: null,
        hzNum: null,
        hzState: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.hzId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加患者信息表";
    },
    /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const hzId = row.hzId || this.ids;
    //   getUser(hzId).then((response) => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改患者信息表";
    //   });
    // },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.hzId != null) {
            updateUser(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hzIds = row.hzId || this.ids;
      this.$modal
        .confirm('是否确认删除患者信息表编号为"' + hzIds + '"的数据项？')
        .then(function () {
          return delUser(hzIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "qiantai/user/export",
        {
          ...this.queryParams,
        },
        `user_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.demo-drawer__content {
  width: 600px;
  padding: 0 50px 20px;
}
::v-deep {
  .pagination-container .el-pagination {
    width: 100%;
    text-align: center;
  }
}
</style>
